/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DBUtils {

    private static String urlDataConnection;
    private static String urlSystemConnection;

    static {
        String url = Config.getDbProperty("url");
        String db_data = Config.getDbProperty("db_UF_DATA");
        String db_system = Config.getDbProperty("db_UF_SYSTEM");
        String user = Config.getDbProperty("user");
        String pwd = Config.getDbProperty("pwd");
        StringBuffer connectionUrlDataStrBuf = new StringBuffer();
        StringBuffer connectionUrlSysStrBuf = new StringBuffer();
        
        connectionUrlDataStrBuf.append(url).append(";");
        connectionUrlDataStrBuf.append("databaseName=").append(db_data).append(";");
        connectionUrlDataStrBuf.append("user=").append(user).append(";");
        connectionUrlDataStrBuf.append("password=").append(pwd);
        
        connectionUrlSysStrBuf.append(url).append(";");
        connectionUrlSysStrBuf.append("databaseName=").append(db_system).append(";");
        connectionUrlSysStrBuf.append("user=").append(user).append(";");
        connectionUrlSysStrBuf.append("password=").append(pwd);
        
        urlDataConnection = connectionUrlDataStrBuf.toString();
        urlSystemConnection = connectionUrlSysStrBuf.toString();
    }

    public static Connection getDataConnection(){
        Connection con = null;
        try {
           con = DriverManager.getConnection(urlDataConnection);
        } catch (SQLException ex) {
            Logger.getLogger(DBUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }
    
    public static Connection getSysConnection(){
        Connection con = null;
        try {
           con = DriverManager.getConnection(urlSystemConnection);
        } catch (SQLException ex) {
            Logger.getLogger(DBUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }

    public static void closePstmt(PreparedStatement pstmt) throws SQLException{
        if(pstmt != null){
            pstmt.close();
        }
    }

    public static void closeAll(ResultSet rs,Statement stmt,Connection con){
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if(stmt != null){
                stmt.close();
                stmt = null;
            }
            if(con != null){
                con.close();
                con = null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   
}
